package com.ding.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ding.commonutils.Result;
import com.ding.eduservice.client.VodClient;
import com.ding.eduservice.entity.EduCourse;
import com.ding.eduservice.entity.EduVideo;
import com.ding.eduservice.entity.vo.Coursequery;
import com.ding.eduservice.entity.vo.PublicCourseQuery;
import com.ding.eduservice.service.EduCourseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.util.calendar.LocalGregorianCalendar;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-16
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private VodClient vodClient;

    @PostMapping("addCourse")
    public Result addCourseInfo(@RequestBody Coursequery coursequery){
        //返回添加之后的课程id，为了后面添加大纲使用
         String id = eduCourseService.saveCourseInfo(coursequery);

        return Result.ok().data("courseId",id);
    }

    //根据课程id查询课程的基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable String courseId){
        Coursequery coursequery = eduCourseService.getCourseInfoById(courseId);
        return Result.ok().data("courseInfo",coursequery);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
        public Result updateCourseInfo(@RequestBody Coursequery coursequery){
         eduCourseService.updateCourseInfo(coursequery);
         return Result.ok();
    }

    @GetMapping("getPublic/{courseId}")
    public Result getPublicInfo(@PathVariable String courseId){
        PublicCourseQuery publicByCourseId = eduCourseService.getPublicByCourseId(courseId);
        return Result.ok().data("getPublic",publicByCourseId);
    }

    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public Result publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return Result.ok();
    }

    //课程列表
    @PostMapping("getCoursedata/{current}/{size}")
    public Result getCoursedata(@PathVariable int current,@PathVariable int size,@RequestBody(required = false) EduCourse eduCourse){
        Page<EduCourse> eduCoursePage = new Page<>(current,size);

        BigDecimal price = eduCourse.getPrice();
        String status = eduCourse.getStatus();
        Date modified = eduCourse.getGmtModified();

        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        if(price != null){
             eduCourseQueryWrapper.gt("price",price);
        }
        if(!StringUtils.isEmpty(status)){
            eduCourseQueryWrapper.eq("status",status);
        }
        if(modified != null){
            eduCourseQueryWrapper.eq("gmt_modified",modified);
        }

         eduCourseService.page(eduCoursePage, eduCourseQueryWrapper);
        List<EduCourse> records = eduCoursePage.getRecords();
        long total = eduCoursePage.getTotal();
        return Result.ok().data("list",records).data("total",total);
    }

    //课程删除
    @DeleteMapping("delCourseInfo/{CourseId}")
    public Result delCourseInfo(@PathVariable String CourseId){
       int del = eduCourseService.delCourseById(CourseId);
       if(del>0){
           return Result.ok();
       }else {
           return Result.error();
       }
    }

}

